package com.lkwlw.tplms.transport.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lkwlw.tplms.mapper.DriverInfoMapper;
import com.lkwlw.tplms.model.DriverInfo;
import com.lkwlw.tplms.order.dao.mapper.OrderinfoMapper;
import com.lkwlw.tplms.order.dao.model.Orderinfo;
import com.lkwlw.tplms.system.dao.model.Admininfo;
import com.lkwlw.tplms.transport.dao.mapper.SchedulingResourcesMapper;
import com.lkwlw.tplms.transport.dao.mapper.TransportInfoMapper;
import com.lkwlw.tplms.transport.dao.mapper.VehicleinfoMapper;
import com.lkwlw.tplms.transport.dao.model.SchedulingResources;
import com.lkwlw.tplms.transport.dao.model.SchedulingResourcesExample;
import com.lkwlw.tplms.transport.dao.model.TransportInfo;
import com.lkwlw.tplms.transport.dao.model.TransportInfoExample;
import com.lkwlw.tplms.transport.dao.model.Vehicleinfo;
import com.lkwlw.tplms.transport.dao.model.VehicleinfoExample;
import com.lkwlw.tplms.transport.dao.model.VehicleinfoExample.Criteria;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class OrderDeliveryAction extends ActionSupport {
	private static final long serialVersionUID = 8891856394778656836L;
	private VehicleinfoMapper vehicleinfoMapper;
	private SchedulingResourcesMapper schedulingResourcesMapper;	
	private Vehicleinfo vehicleinfo;
	private SchedulingResources sched;	
	private DriverInfoMapper driverInfoMapper;
	private OrderinfoMapper orderInfoDAO;	
	private TransportInfoMapper transportInfoMapper;
	private String orderId;	
	private String errorMessage;
	private TransportInfo transportInfo;
	private Orderinfo order;
	private List<Map<String,Object>> sches;
	
	public String execute(){
		ActionContext context = ActionContext.getContext();
		Map<String,Object> session = context.getSession();
		Admininfo admin = (Admininfo) session.get("admin");
		if (null != admin) {
			VehicleinfoExample example = new VehicleinfoExample(); 
			// 根据当前登录司机或配送员用户身份证号获取司机信息
			DriverInfo driver = driverInfoMapper.selectByPrimaryKey(admin.getCardNo());
			if (null != driver) {
				Map paramMap = new HashMap();
				paramMap.put("vehicleNo", driver.getVehicleNo());
				sches = schedulingResourcesMapper.selectSchedulingResources(paramMap);
				Criteria criteria = example.createCriteria();
				criteria.andVehiclenoEqualTo(driver.getVehicleNo());
				// 根据司机信息中车辆编号获取车辆信息
				vehicleinfo = vehicleinfoMapper.selectByPrimaryKey(driver.getVehicleNo());				
				//selectLoadOrderList = transportInfoMapper.selectLoadOrderList(paramMap);
				// 查询获得车辆线路
				SchedulingResourcesExample example2 = new SchedulingResourcesExample();
				com.lkwlw.tplms.transport.dao.model.SchedulingResourcesExample.Criteria criteria2 =
						example2.createCriteria();
				criteria2.andVehicleIdEqualTo(driver.getVehicleNo());
				criteria2.andStatusEqualTo(1);
				example2.setOrderByClause("id desc");
				if (null != schedulingResourcesMapper.selectByExample(example2)) {
					sched = schedulingResourcesMapper.selectByExample(example2).get(0);// 车辆线路
					session.put("sched", sched);
				}
			}
			
		}
		return SUCCESS;
	}
	
	public String unLoadOrder()throws Exception {
		order = orderInfoDAO.selectByPrimaryKey(orderId);
		if (null != order) {
			ActionContext context = ActionContext.getContext();
			Map<String,Object> session = context.getSession();
			sched = (SchedulingResources) session.get("sched");
			Admininfo admin = (Admininfo) session.get("admin");
			TransportInfoExample example = new TransportInfoExample();
			com.lkwlw.tplms.transport.dao.model.TransportInfoExample.Criteria criteria 
				= example.createCriteria();
			criteria.andOrderidEqualTo(orderId);
			criteria.andUnloadEqualTo(2);
			criteria.andTransportnoEqualTo(String.valueOf(sched.getId()));
			List<TransportInfo> tplist = transportInfoMapper.selectByExample(example);
			
			//判断是否重复卸车
			if (null!=tplist && 0 < tplist.size()) {
				errorMessage = "duplicate_unload";	
				transportInfo = transportInfoMapper.selectByExample(example).get(0);
				execute();
			}else{					
				transportInfo.setId(null);
				transportInfo.setOrderid(orderId);
				transportInfo.setTransportno(String.valueOf(sched.getId()));
				transportInfo.setUnload(2);
				String remark = "";
				String orderState = "";
				if (admin.getJob().equals("配送员")) {
					if (order.getState().equals("已收件")) {
						remark = "订单已扫描下车，准备入库";
					}else if (order.getState().equals("派送中")){
						remark = "订单已扫描下车,已签收";
						orderState = "已完成";
					}
				}else{
					remark = "订单已卸车，到达" + sched.getDestination();
				}
				transportInfo.setRemark(remark);
				if (!"".equals(orderState)) {
					// 更新订单状态和订单签收日期			
					order.setState(orderState);
					Date receiveDate = Calendar.getInstance().getTime();
					order.setReceivedate(receiveDate);
					orderInfoDAO.updateStateByPrimaryKey(order);
				}				
				transportInfoMapper.insertSelective(transportInfo);			
				transportInfo = transportInfoMapper.selectByExample(example).get(0);
				execute();
				errorMessage = "success";	
			}			
		}else {
			errorMessage = "norecord";
		}
		orderId = "";
		return SUCCESS;
	}
	
	public void setVehicleinfoMapper(VehicleinfoMapper vehicleinfoMapper) {
		this.vehicleinfoMapper = vehicleinfoMapper;
	}

	public Vehicleinfo getVehicleinfo() {
		return vehicleinfo;
	}
	public void setSchedulingResourcesMapper(SchedulingResourcesMapper schedulingResourcesMapper) {
		this.schedulingResourcesMapper = schedulingResourcesMapper;
	}
	public SchedulingResources getSched() {
		return sched;
	}
	
	public void setVehicleinfo(Vehicleinfo vehicleinfo) {
		this.vehicleinfo = vehicleinfo;
	}

	public void setSched(SchedulingResources sched) {
		this.sched = sched;
	}

	public void setDriverInfoMapper(DriverInfoMapper driverInfoMapper) {
		this.driverInfoMapper = driverInfoMapper;
	}
	
	public void setOrderInfoDAO(OrderinfoMapper orderInfoDAO) {
		this.orderInfoDAO = orderInfoDAO;
	}

	public void setTransportInfoMapper(TransportInfoMapper transportInfoMapper) {
		this.transportInfoMapper = transportInfoMapper;
	}
	
	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public TransportInfo getTransportInfo() {
		return transportInfo;
	}

	public void setTransportInfo(TransportInfo transportInfo) {
		this.transportInfo = transportInfo;
	}

	public Orderinfo getOrder() {
		return order;
	}

	public void setOrder(Orderinfo order) {
		this.order = order;
	}	

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	
	public List<Map<String, Object>> getSches() {
		return sches;
	}
}
